from tests.base_test import base_test_case # 导入统一配置并初始化环境变量
from pathlib import Path

TEST_DATA_DIR = Path(__file__).parent / "test_files"
SUB_DIR = TEST_DATA_DIR / "1715339805571"

if __name__ == '__main__':
    import pdfplumber

    input_path = TEST_DATA_DIR / "1715339805571.pdf"
    with pdfplumber.open(input_path) as pdf:
        text = ""
        for page in pdf.pages:
            text = page.extract_text()
            # text += text
            print(text)
    # =================
    from paddlex.utils.deps import is_dep_available

    if is_dep_available("pypdfium2"):
        import pypdfium2 as pdfium
    pdf = pdfium.PdfDocument(TEST_DATA_DIR / "1715339805571.pdf")
    version = pdf.get_version()  # get the PDF standard version
    n_pages = len(pdf)  # get the number of pages in the document
    page = pdf[0]  # load a page

    # # ** 提取和搜索文本
    # # 加载文本页面帮助程序
    # textpage = page.get_textpage()
    #
    # # 从整个页面中提取文本
    # text_all = textpage.get_text_bounded()
    # # 从特定矩形区域提取文本
    # text_rect = textpage.get_text_bounded(left=50, bottom=100, right=width - 50, top=height - 100)
    # # 从特定字符范围中提取文本
    # text_span = textpage.get_text_range(index=10, count=15)
    #
    # # 在页面上查找文本
    # searcher = textpage.search("something", match_case=False, match_whole_word=False)
    # # 这将返回下一次匹配项 （char_index， char_count），如果未找到，则返回 None
    # match = searcher.get_next()

    # ** 阅读目录
    import pypdfium2.internal as pdfium_i

    for bm in pdf.get_toc(max_depth=2):
        count, dest = bm.get_count(), bm.get_dest()
        out = "    " * bm.level
        out += "[%s] %s -> " % (
            f"{count:+}" if count != 0 else "*",
            bm.get_title(),
        )
        if dest:
            index, (view_mode, view_pos) = dest.get_index(), dest.get_view()
            out += "%s  # %s %s" % (
                index + 1 if index != None else "?",
                pdfium_i.ViewmodeToStr.get(view_mode),
                round(view_pos, 3),
            )
        else:
            out += "_"
        print(out)
    pass
